using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Commonplace.Domain;

namespace Commonplace.Controller
{
    public class FileTypeRecognizer
    {
        static FileTypeRecognizer _instance = new FileTypeRecognizer();

        public static FileTypeRecognizer Instance { get { return _instance; } }

        protected FileTypeRecognizer() { }

        public Type GetFileType(string filename)
        {
            FileInfo fi = new FileInfo(filename);

            switch (fi.Extension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                case ".gif":
                case ".png":
                    return typeof(ImageDetailRecord);
                case ".ogg":
                case ".mid":
                case ".midi":
                    return typeof(AudioDetailRecord);
                case ".svg":
                case ".xcf":
                case ".sxd":
                case ".pdf":
                case ".djvu":
                case ".sxw":
                case ".sxi":
                case ".sxc":
                    //even if other file types are audio or image, if we're here that means the other
                    //detail records and renderers can't deal with those types yet
                    return typeof(FileDetailRecord);
                default:
                    throw new ArgumentException("Not a recognized or processed file type");
            }
        }

        public string GetMimeType(string extension)
        {
            switch (extension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    return "image/jpeg";

                case ".gif":
                    return "image/gif";

                case ".png":
                    return "image/png";

                case ".svg":
                    return "image/svg+xml";

                case ".ogg":
                    return "application/ogg";

                case ".mid":
                case ".midi":
                    return "audio/midi";

                case ".sxd":
                    return "application/vnd.sun.xml.draw";
                case ".sxw":
                    return "application/vnd.sun.xml.writer";
                case ".sxi":
                    return "application/vnd.sun.xml.impress";
                case ".sxc":
                    return "application/vnd.sun.xml.calc";

                case ".djvu":
                case ".djv":
                    return "image/vnd.djvu";

                case ".pdf":
                    return "application/pdf";

                case ".xcf":
                    return "image/x-xcf";

                default:
                    return "application/octet-stream";
            }
        }

        public string FileSelectionMask
        {
            get
            {
                StringBuilder mask = new StringBuilder();

                mask.Append("All Files (Images, Audio and Other)|*.jpg;*.jpeg;*.gif;*.png;*.svg;*.ogg;*.mid;*.midi;*.sxd;*.sxw;*.sxi;*.sxc;*.djvu;*.djv;*.pdf;*.xcf|");
                mask.Append("All Images (JPEG, GIF, PNG, SVG)|*.jpg;*.jpeg;*.gif;*.png;*.svg|");
                mask.Append("JPEG Images (*.jpg, *.jpeg)|*.jpg;*.jpeg|");
                mask.Append("GIF Images (*.gif)|*.gif|");
                mask.Append("PNG Images (*.png)|*.png|");
                mask.Append("SVG Images (*.svg)|*.svg|");
                mask.Append("All Audio Files (OGG, MIDI)|*.ogg;*.mid.*.midi|");
                mask.Append("OGG Audio or Video (*.ogg)|*.ogg|");
                mask.Append("MIDI Audio (*.mid, *.midi)|*.mid;*.midi|");
                mask.Append("All Other Files (OpenOffice, DjVU, PDF, XCF)|*.sxd;*.sxw;*.sxi;*.sxc;*.djvu;*.djv;*.pdf;*.xcf|");
                mask.Append("OpenOffice Files (*.sxd, *.sxw, *.sxi, *.sxc)|*.sxd;*.sxw;*.sxi;*.sxc|");
                mask.Append("DjVU Files (*.djvu)|*.djvu;*.djv|");
                mask.Append("PDF Documents (*.pdf)|*.pdf|");
                mask.Append("XCF Files (*.xcf)|*.xcf");

                return mask.ToString();
            }
        }

    }
}
